﻿Imports System
Imports System.Collections.Generic
Imports System.DirectoryServices

Namespace System.Workflow.Activities
    <Serializable> _
    Friend NotInheritable Class DirectoryLocalQuery
        Implements IDirectoryOperation
        ' Methods
        Public Sub New(ByVal name As String, ByVal value As String, ByVal operation As DirectoryQueryOperation)
            If (name Is Nothing) Then
                Throw New ArgumentNullException("name")
            End If
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value")
            End If
            Me.m_name = name
            Me.m_value = value
            Me.m_operation = operation
        End Sub

        Public Sub GetResult(ByVal rootEntry As DirectoryEntry, ByVal currentEntry As DirectoryEntry, ByVal response As List([Of] DirectoryEntry))
            If (rootEntry Is Nothing) Then
                Throw New ArgumentNullException("rootEntry")
            End If
            If (currentEntry Is Nothing) Then
                Throw New ArgumentNullException("currentEntry")
            End If
            If (response Is Nothing) Then
                Throw New ArgumentNullException("response")
            End If
            [Using] searcher As DirectorySearcher = New DirectorySearcher(currentEntry)
                Dim str As String = "("
                Dim str2 As String = ""
                Dim str3 As String = ")"
                Select Case Me.m_operation
                    Case DirectoryQueryOperation.Equal
                        str2 = "="
                        Exit Select
                    Case DirectoryQueryOperation.NotEqual
                        str = "(!("
                        str2 = "="
                        str3 = "))"
                        Exit Select
                End Select
                searcher.Filter = String.Concat(New String() { str, Me.m_name, str2, Me.m_value, str3 })
                Dim result As SearchResult
                For Each result In searcher.FindAll
                    response.Add(result.GetDirectoryEntry)
                Next
            End [Using]
        End Sub


        ' Fields
        Friend m_name As String
        Friend m_operation As DirectoryQueryOperation
        Friend m_value As String
    End Class
End Namespace

